package han.temp;

import com.han.http.fetch.Fetcher;
import han.entity.FinanceProjectBaseInfoEntity;
import han.sql.Operator;
import han.util.ExcelUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 * @author: Han
 * @date: 2018/7/4 15:34
 * @dscription: TODO
 */
public class TempExporter {

    private Operator operator;

    private TempExporter(boolean b) {
        Fetcher fetcher = new Fetcher();
        operator = Operator.getInstance();
    }

    public TempExporter() {
    }

    public static void main(String... args) {
        TempExporter tempExporter = new TempExporter(true);
        tempExporter.exportProjectInfo();
    }

    private void exportProjectInfo() {
        Map<String, String> companyIdAreaMap = loadIdAreaMap();
        List<FinanceProjectBaseInfoEntity> baseInfoEntityList = new ArrayList<>();//operator.queryAllProjectInfo();
        List<Map<String, String>> infoEntityList = new ArrayList<>();
        Map<String, List<Map<String, String>>> sheetMap = new LinkedHashMap<>();
        for (FinanceProjectBaseInfoEntity baseInfoEntity : baseInfoEntityList) {
            try {
                String krId = baseInfoEntity.getKrId();
                String area = companyIdAreaMap.get(krId);
                baseInfoEntity.setArea(area);
                /*List<FinanceProjectFinanceEntity> financeEntityList = operator.queryFinanceById(krId);
                List<FinanceProjectMemberEntity> memberEntityList = operator.queryMemberById(krId);
                List<FinanceProjectNewsEntity> newsEntityList = operator.queryNewsById(krId);
                TempFinanceInfoEntity infoEntity = new TempFinanceInfoEntity(baseInfoEntity,financeEntityList,
                        memberEntityList,newsEntityList);
                Map<String, String> map = infoEntity.generateMap();
                infoEntityList.add(map);*/
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sheetMap.put("sheet1", infoEntityList);
        ExcelUtils.writeToLocal("F:\\融资项目信息.xls", sheetMap);
    }

    private Map<String, String> loadIdAreaMap() {
        Map<String, String> areaMap = new LinkedHashMap<>();
        areaMap.put("101", "北京市");
        areaMap.put("109", "上海市");
        areaMap.put("21903", "深圳市");
        areaMap.put("21901", "广州市");
        areaMap.put("21101", "杭州市");
        areaMap.put("22301", "成都市");
        areaMap.put("102", "天津市");
        areaMap.put("90000", "海外");
        areaMap.put("122", "重庆市");
        areaMap.put("112", "安徽省");
        areaMap.put("113", "福建省");
        areaMap.put("128", "甘肃省");
        areaMap.put("119", "广东省");
        areaMap.put("120", "广西");
        areaMap.put("124", "贵州省");
        areaMap.put("121", "海南省");
        areaMap.put("103", "河北省");
        areaMap.put("116", "河南省");
        areaMap.put("108", "黑龙江省");
        areaMap.put("117", "湖北省");
        areaMap.put("118", "湖南省");
        areaMap.put("107", "吉林省");
        areaMap.put("110", "江苏省");
        areaMap.put("114", "江西省");
        areaMap.put("106", "辽宁省");
        areaMap.put("130", "宁夏");
        areaMap.put("129", "青海省");
        areaMap.put("115", "山东省");
        areaMap.put("104", "山西省");
        areaMap.put("127", "陕西省");
        areaMap.put("123", "四川省");
        areaMap.put("125", "云南省");
        areaMap.put("111", "浙江省");
        areaMap.put("105", "内蒙古");
        areaMap.put("131", "新疆");
        areaMap.put("126", "西藏");
        areaMap.put("133", "香港");
        areaMap.put("134", "澳门");
        areaMap.put("132", "台湾");
        Set<String> keySet = areaMap.keySet();
        Map<String, String> companyIdAreaMap = new LinkedHashMap<>();
        for (String areaId : keySet) {
            Map<String, String> tempMap = loadIdAreaMapById(areaId, areaMap.get(areaId));
            companyIdAreaMap.putAll(tempMap);
        }

        return companyIdAreaMap;
    }

    private Map<String, String> loadIdAreaMapById(String areaId, String area) {
        BufferedReader reader = null;
        Map<String, String> map = new LinkedHashMap<>();
        try {
            reader = new BufferedReader(new FileReader("F:\\数据\\36ke\\CompanyId\\CompanyId - " + areaId + ".txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                map.put(line.replaceAll("\\s", ""), area);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(reader).close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return map;
    }
}
